package edu.nyu.hps.architecture;

import java.util.ArrayList;

/**
 * This is a child class of class player It handles data for the CAT player
 * 
 * @author hiralrajani
 * 
 */
public class Cat extends Player {

	
	int opponentX, opponentY;

	public Cat(int x, int y) {
		super();
		this.x = x;
		this.y = y;
		this.who = "cat";
		this.time = 120;
		this.ready = false;
		this.totalMoves = 0;
		this.caught = false;
	}

	@Override
	public boolean validMove(String[] moves) {

		if (moves.length > 5 || moves.length < 2) {
			return false;
		}
		String[] currMove = moves[1].split(",");
		int mx = Integer.parseInt(currMove[0]);
		int my = Integer.parseInt(currMove[1]);

		if (moves.length == 2) {
			if (!isInBoundary(mx, my)) {
				System.out.println("Cat move not in boundary");
				return false;
			}
			if (isMoreThanOneStep(mx, my, x, y)) {
				System.out.println("Cat took more than one step");
				return false;
			}
			return true;
		}
		if (!isInBoundary(mx, my)) {
			System.out.println("Cat move not in boundary");
			return false;
		}
		if (isMoreThanOneStep(mx, my, x, y)) {
			System.out.println("Cat took more than one step");
			return false;
		}
		
		int changeCount = 0;
		mx = mx - x;
		my = my - y;
		int tmx, tmy;
		int tx, ty, tpx, tpy;
		for (int i = 2; i < moves.length; i++) {
			String[] prevMove = moves[i - 1].split(",");
			currMove = moves[i].split(",");
			tx = Integer.parseInt(currMove[0]);
			tpx = Integer.parseInt(prevMove[0]);
			ty = Integer.parseInt(currMove[1]);
			tpy = Integer.parseInt(prevMove[1]);
			tmx = tx - tpx;
			tmy = ty - tpy;

			if (!isInBoundary(tx, ty)) {
				System.out.println("Cat move not in boundary");
				return false;
			}
			if (isMoreThanOneStep(tx, ty, tpx, tpy)) {
				System.out.println("Cat took more than one step");
				return false;

			}
			if (((tmx != mx || tmy != my) && (tx != tpx || ty != tpy)) && (tmx !=0 && tmy!=0)) {
				changeCount++;
			}
			mx = tmx;
			my = tmy;
		}

		if (changeCount > 1) {
			System.out.println("Cat changed direction more than once");
			return false;
		}
		return true;
	}

	private boolean isMoreThanOneStep(int x, int y, int x1, int y1) {
		if (Math.abs(x - x1) > 1 || Math.abs(y - y1) > 1) {
			return true;
		}
		return false;
	}

	public boolean validMove1(String[] move) {

		// One extra because move[0] holds "MOVE"
		if (move.length > 5 || move.length < 2) {
			return false;
		}
		int prevX = this.x, prevY = this.y, totalDirChange = 0, currDirection = 0, prevDirection = 0;
		;

		for (int i = 1; i < move.length; i++) {
			String[] coordinates = move[i].split(",");
			int x = Integer.parseInt(coordinates[0]);
			int y = Integer.parseInt(coordinates[1]);

			if (!isInBoundary(x, y)) {
				System.out.println(who + " not in boundary!");
				return false;
			}

			if (Math.abs(x - prevX) > 1 || Math.abs(y - prevY) > 1) {
				System.out.println(who + " took more than one step!");
				return false;
			}

			// Tracking the direction
			if (currDirection != 0) {
				prevDirection = currDirection;
			}
			currDirection = getCurrDirection(x, y, prevX, prevY);
			if (i > 1 && prevDirection != currDirection) {
				totalDirChange++;
			}
			if (totalDirChange > 1) {
				System.out.println(who + " changed direction more than once");
				return false;
			}
			prevX = x;
			prevY = y;
		}
		return true;
	}

	
//	@Override
	public String trackMove(String[] input) {
		if(validMove(input)){
			
		}else{
			System.out.println("NACK:Invalid Move");
			return "NACK:Invalid Move";
		}
		return null;
	}
	/**
	 * Return the current direction of cat
	 * 
	 * @param x
	 * @param y
	 * @param prevX
	 * @param prevY
	 * @return
	 */
	private int getCurrDirection(int x, int y, int prevX, int prevY) {
		int right = 1, up = 2, left = 3, down = 4, diagonal = 5;

		if (x == prevX + 1 && y == prevY) {
			// System.out.println(who + " went right");
			return right;
		} else if (x == prevX && y == prevY + 1) {
			// System.out.println(who + " went up");
			return up;
		} else if (x == prevX - 1 && y == prevY) {
			// System.out.println(who + " went left");
			return left;
		} else if (x == prevX && y == prevY - 1) {
			// System.out.println(who + " went down");
			return down;
		} else if (Math.abs(x - prevX) == 1 && Math.abs(y - prevY) == 1) {
			// System.out.println(who + " wen diagonal");
			return diagonal;
		}
		return 0;
	}

	/**
	 * Keeps track of the current location of the opponent
	 */
	public void setOpponentLoc(int x, int y) {
		this.opponentX = x;
		this.opponentY = y;
	}

	/**
	 * This method is for the BOT program.
	 */
	public String clientMove(int currX, int currY) {

		// return "MOVE:" + x + "," + (y+1) + ":" + x + "," + (y+1) + ":" +
		// (x+1)
		// + "," + y + ":" + (x+1) + "," + y;
		CatMoves cm = new CatMoves(currX, currY, opponentX, opponentY);
		return cm.getMove();
	}

}
